﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Model;

namespace BLL
{
    public class DataSetHelper
    {
        public static void RowToObject(List<ColumnDefine> cols, Control obj)
        {
            foreach (Control ctrl in obj.Controls)
            {

                if (ctrl is GroupBox || ctrl is Panel || ctrl is TabControl||ctrl is TabPage)
                    RowToObject(cols, ctrl);
                else if (ctrl is TextBox)
                {
                    ctrl.Text = GetColValue(ctrl.Name, cols).ToString();
                }
                else if(ctrl is ComboBox)
                {
                    ((ComboBox)ctrl).SelectedValue = GetColValue(ctrl.Name, cols).ToString();
                }
                else if (ctrl is CheckBox)
                {
                    bool b=false;
                    if(!string.IsNullOrEmpty(GetColValue(ctrl.Name, cols).ToString()))
                     b= Convert.ToBoolean(GetColValue(ctrl.Name, cols).ToString());
                   ((CheckBox)ctrl).Checked = b;
                }

               

            }
        }
        private static object GetColValue(string ctrlName, List<ColumnDefine> cols)
        {
            foreach(ColumnDefine col in cols)
            {
                if (col.ControlName == ctrlName)
                {
                    return col.Value==null?string.Empty:col.Value;
                }
            }
            return string.Empty;
        }
        private static void SetColValue(object value, string ctrlName, List<ColumnDefine> cols)
        {
            int length=cols.Count;
           for(int i=0;i<length;i++)
           {
                if(cols[i].ControlName==ctrlName)
                {
                     cols[i].Value = value;
                }
            }
            
        }
        public static void GetValue(List<ColumnDefine> cols, Control c)
        {
            foreach (Control ctrl in c.Controls)
            {

                if (ctrl is GroupBox || ctrl is Panel || ctrl is TabControl || ctrl is TabPage)
                    GetValue(cols, ctrl);
                else if (ctrl is TextBox)
                {
                    SetColValue(ctrl.Text, ctrl.Name, cols);
                }
                else if (ctrl is ComboBox)
                {
                    SetColValue(((ComboBox)ctrl).SelectedValue, ctrl.Name, cols);
                }
                else if (ctrl is CheckBox)
                {
                    SetColValue(((CheckBox)ctrl).Checked, ctrl.Name, cols);
                    
                }



            }
        }
    }
}
